package com.qifan.alb2b.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import com.qifan.alb2b.action.front.param.OrderQueryParam;
import com.qifan.alb2b.dao.CompanyInfoDao;
import com.qifan.alb2b.dao.OrderInfoDao;
import com.qifan.alb2b.dao.TravelInfoDao;
import com.qifan.alb2b.dao.UserInfoDao;
import com.qifan.alb2b.domain.CompanyInfoDomain;
import com.qifan.alb2b.dao.RouteDetailInfoDao;
import com.qifan.alb2b.dao.RouteInfoDao;
import com.qifan.alb2b.domain.OrderInfoDomain;
import com.qifan.alb2b.domain.RouteInfoDomain;
import com.qifan.alb2b.domain.TravelInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.cache.AppCache;
import com.qifan.alb2b.util.Arith;
import com.qifan.alb2b.service.vo.OrderVo;
import com.qifan.alb2b.service.vo.TravelVo;
import com.qifan.alb2b.service.vo.UserVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

@Service
public class OrderInfoService {
	private OrderInfoDao orderInfoDao;
	private UserInfoDao userInfoDao;
	private CompanyInfoDao companyInfoDao;
	private TravelInfoDao travelInfoDao;
	private RouteInfoDao routeInfoDao;

	public void setOrderInfoDao(OrderInfoDao orderInfoDao) {
		this.orderInfoDao = orderInfoDao;
	}
	
	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	public void setCompanyInfoDao(CompanyInfoDao companyInfoDao) {
		this.companyInfoDao = companyInfoDao;
	}

	public void setTravelInfoDao(TravelInfoDao travelInfoDao) {
		this.travelInfoDao = travelInfoDao;
	}

	public Page query(OrderQueryParam oqp,DataGridModel dgm){
		return orderInfoDao.query(oqp, dgm);
	}
	
	public void setRouteInfoDao(RouteInfoDao routeInfoDao) {
		this.routeInfoDao = routeInfoDao;
	}

	/**
	 * 对订单进行付款操作
	 * @param orderId
	 */
	public void payOrder(String orderId){
		OrderInfoDomain order=orderInfoDao.getOrderById(orderId);
		if(order==null)
			throw new BusinessException("更新订单状态失败,订单号为"+orderId+"的订单不存在");
		if(order.getOrderStatus()!=Constants.ORDER_STATUS_NOPAY)
			throw new BusinessException("只有订单状态为“未支付”的订单才可以执行此操作，请刷新后重试");
		orderInfoDao.updateStatus(orderId, Constants.ORDER_STATUS_PAYED);//更新状态为已支付		
	}
	
	/**
	 * 确认付款操作
	 * @param orderId
	 */
	public void confirmOrder(String orderId){
		double integralScale=AppCache.configInfoMap.get("integral_scale")==null?
				0:Double.parseDouble(AppCache.configInfoMap.get("integral_scale").getConfigVal());//交易积分的比例
		OrderInfoDomain order=orderInfoDao.getOrderById(orderId);
		if(order==null)
			throw new BusinessException("更新订单状态失败,订单号为"+orderId+"的订单不存在");
		if(order.getOrderStatus()!=Constants.ORDER_STATUS_PAYED)
			throw new BusinessException("只有订单状态为“已支付”的订单才可以执行此操作，请刷新后重拾");
		//这个地方还要做积分、信用额度的更新等操作
		double integral=Arith.roundHalfUp(Arith.mul(order.getOrderTotalAmount(), integralScale),Constants.XIAOSHU);
		integral=integral/2;
		UserInfoDomain user=userInfoDao.getUserById(order.getUserId());
		CompanyInfoDomain comp=companyInfoDao.getCompanyById(order.getCompanyId());
		user.setOrderCount(user.getOrderCount()+1);//设置用户的成功交易订单数
		user.setOrderAmount(user.getOrderAmount()+order.getOrderTotalAmount());//设置用户成功交易金额
		user.setIntegral((int) integral+user.getIntegral());//设置用户积分
		comp.setOrderCount(comp.getOrderCount()+1);//公司
		comp.setOrderAmount(comp.getOrderAmount()+order.getOrderTotalAmount());//公司
		comp.setIntegral((int) integral+comp.getIntegral());//设置公司积分
		userInfoDao.update(user);
		companyInfoDao.update(comp);
		orderInfoDao.updateStatus(orderId, Constants.ORDER_STATUS_OK);//更新状态为已支付
	}
	
	/**
	 * 取消订单操作
	 * @param orderId
	 */
	public void cancelOrder(String orderId){
		OrderInfoDomain order=orderInfoDao.getOrderById(orderId);
		if(order==null)
			throw new BusinessException("更新订单状态失败,订单号为"+orderId+"的订单不存在");
		if(order.getOrderStatus()!=Constants.ORDER_STATUS_NOPAY)
			throw new BusinessException("只有订单状态为“未支付”的订单才可以执行此操作，请刷新后重拾");
		orderInfoDao.updateStatus(orderId, Constants.ORDER_STATUS_CANCEL);//更新状态为已取消
	}
	
	
	
	public OrderVo queryOrderVo(String orderId){
		OrderInfoDomain orderInfo=orderInfoDao.getOrderById(orderId);
		List<TravelInfoDomain> travelList=travelInfoDao.query(orderId);
		UserInfoDomain userInfo=userInfoDao.getUserById(orderInfo.getUserId());
		CompanyInfoDomain compInfo=companyInfoDao.getCompanyById(orderInfo.getCompanyId());
		RouteInfoDomain routeInfo=routeInfoDao.getRouteById(orderInfo.getRouteId());
		
		UserVo userVo=new UserVo();
		userVo.setCompanyInfo(compInfo);
		userVo.setUserInfo(userInfo);
		
		List<TravelVo> travelVoList=new ArrayList<TravelVo>();
		for(int i=0;i<travelList.size();i++){
			TravelVo travelVo=new TravelVo();
			travelVo.setTravelInfo(travelList.get(i));
			travelVoList.add(travelVo);
		}
		
		OrderVo orderVo=new OrderVo();
		orderVo.setOrderInfo(orderInfo);
		orderVo.setUserVo(userVo);
		orderVo.setTravelList(travelVoList);
		orderVo.setRouteInfo(routeInfo);
		
		return orderVo;
	}
	
}
